import numpy as np


input_example = [[-0.803556, 1.803556, 0.016222, 0.0, 0.0, 0.004444, 0.000444, 0.052444, 0.097556, 8450139.022222, 0.131081, 0.004413, 0.072321, 0.868919, 0.00305, 0.004159, 1209008640.0, 478536192.0, 31802.4, 32218.6, 22029476.5, 8450139.022222],
 [-0.891333, 1.891333, 0.035778, 0.0, 0.0, 0.0, 0.0, 0.027556, 0.034444, 198337.422222, 0.076584, 0.003422, 0.047097, 0.923416, 0.005257, 0.003815, 598827520.0, 925864448.0, 2583.266667, 3061.8, 3030607.5, 198337.422222],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 5677448.647111, 0.07524, 0.0034, 0.047145, 0.92476, 0.005285, 0.003765, 604049920.0, 923632128.0, 0, 0, 3011592.0, 5677448.647111],
 [0, 0, 0, 0, 0, 0, 0, 0, 0, 16791924736.0, 0.146394, 0.006558, 0.087674, 0.853606, 0.003772, 0.004642, 1381130752.0, 1922301952.0, 0, 0, 22523937.0, 16791924736.0]]

output_example = [[-8.03556e-01,  1.803556,  1.6222e-02,  0,  0,  -1,  -1,  -1, -1,  8450139.022222,  0.131081,  4.413e-03,
   7.2321e-02,  8.68919e-01,  -1,  4.159e-03,  1.20900864e+09,  -1,  -1,  -1,  2.20294765e+07,  8450139.022222],
 [-1,  -1,  -1,  0,  0,  0,  0,  2.7556e-02,  3.4444e-02,  -1,  7.6584e-02,
  3.422e-03,  -1,  9.23416e-01,  5.257e-03,  3.815e-03,  -1,  9.25864448e+08,  2583.266667,  3.0618e+03,  3.03060750e+06,  -1],
 [0, 0, 0, 0, 0, 0, 0, 0, 0,  5677448.647111,  -1,
  -1,  4.7145e-02,  -1,  -1,  -1,  6.04049920e+08,  9.23632128e+08, 0,  0,  -1,  5677448.647111],
 [0, 0, 0, 0, 0, 0, 0, 0, 0,  -1,  -1,  -1,
  -1, -1,  3.772e-03,  -1,  -1,  -1,  0, 0,  -1,  -1]]



# def compare(o_e, o):
#     assert isinstance(o_e, np.ndarray)
#     assert isinstance(o, np.ndarray)
#
#     ori = 0
#     cur = 0
#     no = o_e.shape[1]
#     for num in range(0, no):
#         ori = ori + np.var(o_e[:, num])
#
#     for num in range(0, no):
#         cur = cur + np.var(o[:, num])
#
#     return ori, cur, cur < ori


def compare(o_e, o):
    assert isinstance(o_e, np.ndarray)
    assert isinstance(o, np.ndarray)
    if o_e.shape == o.shape:

        temp = o_e == o
        flag = True
        for row in temp:
            for col in row:
                flag = flag & col
        return flag
    else:
        return False


def ourpreprocess(temp_arr):
    no=temp_arr.shape[1]
    DSIZE=temp_arr.shape[0]
    for num in range(0,no):
        value=temp_arr[:,num]
        npArray=dataPreprocess(value,DSIZE)
        temp_arr[:, num]=npArray
    return temp_arr


def percent_range(dataset,DSIZE, min=0.20, max=0.80):
    range_max = np.percentile(dataset, max * 100)
    range_min = -np.percentile(-dataset, (1 - min) * 100)

    result=np.empty((DSIZE,))
    i=0
    for value in dataset:
        if value <= range_max and value >= range_min:
            result[i]=dataset[i]
        else:
            result[i]=-1

        i+=1
    return result


def dataPreprocess(npArray,DSIZE):
    npArray = percent_range(npArray, DSIZE, 0.025, 0.975)
    return npArray


# 用例目的
print("该用例目的为：")
print("进行离群点处理组件功能测试，测试该组件在正常数据输入时能否获得预定输出，该处理后，离群点将被删除，用-1补位")

# 子用例编号
print("子用例编号：")
print("Outlier_1")

print("****************************")
print("当前输入为：")
# 输出用例设置
print(input_example)
# 输出用例设置

print("")

print("****************************")
print("当前输出为:")
# 输出处理后数据

output = ourpreprocess(np.array(input_example))
print(output)
# 输出处理后数据

print("****************************")
print("是否正确:")
# 输出对比结果
# 需要写一个compare函数
data = compare(np.array(output_example), output)
if data:
    # print(f"原数组方差为：${data[0]}")
    # print(f"当前数组方差为：${data[1]}，小于原始数组")
    print("输出与预定目标相符")
else:
    print("输出与预定目标不符")
# 输出对比结果

print("\n")
